<?php


namespace app\admin\controller\api;


use app\BaseController;
use QL\QueryList;
use think\facade\Db;

/*
 * 采集类
 * */
class Gather extends BaseController
{

    public function index(){
/*       for ($i = 1 ; $i<=303;$i++){
           $this->getGoods($i);
       }*/
//        $this->getGoods(40);
    }

    /*
     * 获取物品
     * */
    public function getGoods($page){
        $ql = QueryList::get("http://tbc.60wdb.com/items?p={$page}");

        $html = $ql->find('.table-responsive')->html();

        $table = QueryList::html($html)->find('table');
        // 采集表的每行内容
        $tableRows = $table->find('tr:gt(0)')->map(function($row){
            return $row->find('td')->htmls()->all();
        });
//        dump($tableRows->all());
        //处理表单的全部数据
        foreach ($tableRows->all() as $key=>$val){
//            dump($val);
            //获取图片和id
            $imgs = QueryList::html($val[0]);
            //ID
            $data['goods_id'] = $imgs->find('a')->attr('href');
            $data['goods_id'] = explode("/", $data['goods_id']);
            $data['goods_id'] = $data['goods_id'][4]; //物品id
            //图片
            $data['img'] = $imgs->find('img')->attr('src'); //物品图片
            $res = file_get_contents($data['img']);
            // 固定保存到指定文件夹中
            $suffix= substr($data['img'],strrpos($data['img'],'.'));
            $info = './static/wow/goods/'.$data['goods_id'].$suffix;
            file_put_contents($info,$res);
            $data['img'] = "/static/wow/goods/".$data['goods_id'].$suffix; //图片路径
            //获取名称
            $data['title'] = QueryList::html($val[1])->find('a:eq(0)')->text();
            //获取装等
            $data['equip_max'] = QueryList::html($val[3])->find('span')->text();
            //获取要求
            $data['demand'] = QueryList::html($val[4])->find('span')->text();
            //获取品质
            $data['quality'] = QueryList::html($val[5])->find('span')->text();
            //获取来源
            $data['source'] = $val[6];
            //获取类别
            $data['type'] = QueryList::html($val[7])->find('span')->text();
            //获取部位
            $data['position'] = QueryList::html($val[8])->find('span')->text();
//            dump($data);
            Db::name('wow_goods')->insert($data);
        }
        dump("爬取第{$page}完毕");
    }

    /*
     * 爬起物品详情
     * */
    public function goodsDesc(){
        $list = Db::name('wow_goods')->page(1,10)->select();

        foreach ($list as $key=>$val){
            $ql = QueryList::get("http://tbc.60wdb.com/item/{$val['goods_id']}/dropped_by");
            $htmls = $ql->find('.item_right')->html();
//            dump($html);
            $html = QueryList::html($htmls);
            $data['goods_id'] = $html->find('.q0')->text(); //id
            $data['goods_id'] = explode(" ", $data['goods_id'])[1];
            // 获取a元素对象
            $link = $html->find('a');
            // 设置元素属性值
            $link->attr('href','#');
            $link->attr('alt','百度');

            //移除元素
            $html->find('.li:last')->remove();
            $htmls = $html->find('.item')->html();

            $style = "<style>
                            .li{
                                color: #303133;
                                font-size: 14px;
                                padding: 2px;
                            }
                            a {
                                text-decoration: none;
                                 color: #606266;
                            }
                            .cnname{
                                font-weight: bold;
                                font-size: 18px;
                                color: #2979ff;
                            }
                            .color0{
                                color: #c0c4cc;
                            }
                            .color7{
                                color: #f29100;
                            }
                            .color11{
                                color: #19be6b;
                            }
                            .right{
                                float: right;
                            }
                      </style>";
/*            dump($htmls.$style);
            echo $htmls.$style;
            die();*/
            $data['content'] = $htmls.$style;
            Db::name('wow_goods_desc')->insert($data);
        }
        dump("爬取完毕");
    }

    /*
     * wow全区金价
     * */
    public function gold(){
        for ($i = 0;$i < 200;$i++){
            $urls = "https://www.dd373.com/s-eja7u2-0-0-0-0-0-jk5sj0-0-0-0-0-0-{$i}-0-0-0.html";
            $ql = QueryList::get($urls);
            $html = $ql->find('.good-list-box')->html();
            $this->goldList($html);
        }
    }

    /*
     * 处理金价列表
     * */
    public function goldList($html){
        $html = QueryList::html($html);
        for ($i = 0;$i < 20;$i++){
            $oneList =$html->find(".goods-list-item:eq({$i})")->html();
            $list = QueryList::html($oneList);
            $result = $list->find('a')->texts();
            $data['area'] = $result[2];
            $data['name'] = $result[3];
            $data['camp'] = $result[4];
            $res = $list->find('.width233')->texts();
            if(isset($res[0])){
                $scale = explode("\r\n", $res[0]);
                $data['scale'] = trim($scale[0]);
                $res = explode("=", $data['scale']);
                $data['scale'] = explode("金", $res[1])[0];
                $data['scale1'] = trim($scale[1]);
            }
            $data['price'] = $list->find('.game-account-flag')->texts()[0];
            $map = [
                'camp' =>$data['camp'],
                'name' => $data['name']
            ];
            $list = Db::name('wow_gold')->where($map)->find();
            if($list){
                //更新数据
                $data['scale'] < $list['scale'] && Db::name('wow_gold')->where($map)->update([
                    'scale' => $data['scale'],
                    'scale1' => $data['scale1'],
                    'price' => $data['price']
                ]);
            }else{
                Db::name('wow_gold')->insert($data);
            }
        }
    }
}